<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>H5返回测试页面</title>
    <style>
        body {
            margin: 0;
            padding: 20px;
            font-family: Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            color: white;
        }
        .container {
            max-width: 600px;
            margin: 0 auto;
            text-align: center;
        }
        .page {
            display: none;
            padding: 40px 20px;
            background: rgba(255,255,255,0.1);
            border-radius: 15px;
            margin: 20px 0;
            backdrop-filter: blur(10px);
        }
        .page.active {
            display: block;
        }
        button {
            background: rgba(255,255,255,0.2);
            border: 1px solid rgba(255,255,255,0.3);
            color: white;
            padding: 12px 24px;
            border-radius: 25px;
            cursor: pointer;
            margin: 10px;
            font-size: 16px;
            transition: all 0.3s ease;
        }
        button:hover {
            background: rgba(255,255,255,0.3);
            transform: translateY(-2px);
        }
        .info {
            background: rgba(0,0,0,0.2);
            padding: 15px;
            border-radius: 10px;
            margin: 20px 0;
            font-size: 14px;
        }
        h1 {
            margin-bottom: 30px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>H5页面返回测试</h1>
        
        <!-- 页面1 -->
        <div id="page1" class="page active">
            <h2>页面 1 - 首页</h2>
            <p>这是第一个页面</p>
            <button onclick="goToPage(2)">前往页面2</button>
            <button onclick="goToPage(3)">前往页面3</button>
            <div class="info">
                <strong>当前信息：</strong><br>
                历史记录长度: <span id="historyLength">1</span><br>
                当前状态: <span id="currentState">null</span>
            </div>
        </div>

        <!-- 页面2 -->
        <div id="page2" class="page">
            <h2>页面 2 - 详情页</h2>
            <p>这是第二个页面，可以返回上一页</p>
            <button onclick="goToPage(1)">返回首页</button>
            <button onclick="goToPage(3)">前往页面3</button>
            <div class="info">
                <strong>当前信息：</strong><br>
                历史记录长度: <span id="historyLength2">1</span><br>
                当前状态: <span id="currentState2">null</span>
            </div>
        </div>

        <!-- 页面3 -->
        <div id="page3" class="page">
            <h2>页面 3 - 设置页</h2>
            <p>这是第三个页面，可以返回任意页面</p>
            <button onclick="goToPage(1)">返回首页</button>
            <button onclick="goToPage(2)">返回页面2</button>
            <div class="info">
                <strong>当前信息：</strong><br>
                历史记录长度: <span id="historyLength3">1</span><br>
                当前状态: <span id="currentState3">null</span>
            </div>
        </div>
    </div>

    <script>
        let currentPage = 1;

        // 更新信息显示的函数
        function updateInfo() {
            const spans = document.querySelectorAll('[id^="historyLength"]');
            spans.forEach(span => {
                span.textContent = window.history.length;
            });
            
            const stateSpans = document.querySelectorAll('[id^="currentState"]');
            stateSpans.forEach(span => {
                span.textContent = JSON.stringify(window.history.state) || 'null';
            });
        }

        // 页面切换函数
        function goToPage(pageNum) {
            // 隐藏所有页面
            document.querySelectorAll('.page').forEach(page => {
                page.classList.remove('active');
            });
            
            // 显示目标页面
            document.getElementById('page' + pageNum).classList.add('active');
            currentPage = pageNum;
            
            // 更新历史记录（仅在需要时）
            if (pageNum !== 1 || window.history.length === 1) {
                window.history.pushState({page: pageNum}, '', '#page' + pageNum);
            }
            
            updateInfo();
            console.log('切换到页面', pageNum, '历史记录长度:', window.history.length);
        }

        // 监听浏览器返回事件
        window.addEventListener('popstate', function(event) {
            console.log('浏览器返回事件触发', event.state);
            
            // 根据历史状态返回相应页面
            if (event.state && event.state.page) {
                const pageNum = event.state.page;
                document.querySelectorAll('.page').forEach(page => {
                    page.classList.remove('active');
                });
                document.getElementById('page' + pageNum).classList.add('active');
                currentPage = pageNum;
            } else {
                // 如果没有状态，返回首页
                document.querySelectorAll('.page').forEach(page => {
                    page.classList.remove('active');
                });
                document.getElementById('page1').classList.add('active');
                currentPage = 1;
            }
            
            updateInfo();
        });

        // 初始化
        document.addEventListener('DOMContentLoaded', function() {
            updateInfo();
            
            // 如果URL有hash，根据hash导航
            if (window.location.hash) {
                const pageMatch = window.location.hash.match(/#page(\d+)/);
                if (pageMatch) {
                    const pageNum = parseInt(pageMatch[1]);
                    if (pageNum >= 1 && pageNum <= 3) {
                        goToPage(pageNum);
                    }
                }
            }
            
            // 初始状态
            if (window.history.length === 1) {
                window.history.replaceState({page: 1}, '', '#page1');
            }
        });

        // 每2秒更新一次信息
        setInterval(updateInfo, 2000);
    </script>
</body>
</html>